package pl.prv.polanie.client.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import pl.prv.polanie.client.dto.CSSemesterDTO;
import pl.prv.polanie.client.dto.CSStudiesTypeDTO;

/**
 * Klasa gromadzaca metody, dzialajace w oparciu o Cache.
 * @author Janusz Prokulewicz
 * Created: Dec 16, 2008
 */

public class CacheService {
	
	public static boolean isPossibleToCreateGroup() {
		if (Cache.getLoadedSemesterYearGroupsCount() > 0) {
			return true;
		}
		return false;
	}
	
	public static long getGroupIDForGivenSubgroupID(long subgroupID) {
		return Cache.getCSSubgroupDTOByID(subgroupID).getGroupID();
	}
	
	public static boolean hasSelectedSemesterCreatedAnyCalendarDays() {
		return Cache.getAllCalendarDays().length > 0;
	}
	
	/**
	 * Zwraca true, gdy dla podanego groupID istnieje przynajmniej jedna subgroupa.
	 *  
	 * @param groupID
	 * @return
	 */
	public static boolean existsAtLeastOneSubgroupForGivenGroupID(long groupID) {
		return Cache.getSubgroupsByGroupID(groupID).length > 0;
	}

	public static boolean isFullTimeSemesterSelected() {
	//		Planner.log("ContextSettings.isFullTimeSemesterSelected: " + "id " + semesterID);
		CSSemesterDTO semesterDTO = Cache.getCSSemesterDTOByID(ContextSettings.semesterID);
		CSStudiesTypeDTO studiesTypeDTO = Cache.getCSStudiesTypeDTOByID(semesterDTO.getStudiesTypeID());
			
		if (studiesTypeDTO.getId() == 2) {
			//2 => zaoczne
			//1 => dzienne, 3 => wieczorowe
			return false;
		}
		return true;
	}
	
	
	
	
	public static long getSemesterIDForLatestSemester(boolean showSemestersVisibleForOrdinaryUsersOnly) {
		CSSemesterDTO[] allSemesters = Cache.getAllSemesters();
		
		List<CSSemesterDTO> semestersList = new ArrayList<CSSemesterDTO>();
		

		//sorts semesters
		for (int i = 0; i < allSemesters.length; i++) {
			CSSemesterDTO semesterDTO = allSemesters[i];
			if (!showSemestersVisibleForOrdinaryUsersOnly || semesterDTO.isVisibleForOrdinaryUsers()) {
				semestersList.add(semesterDTO);
			}
		}
		
		 
		if (semestersList.size() == 0) return -1;

		
		Comparator<CSSemesterDTO> comp = new Comparator<CSSemesterDTO> (){
			public int compare(CSSemesterDTO o1, CSSemesterDTO o2) {
				if (o1.getYearNumber() > o2.getYearNumber()) {
					return 1;
				}
				if (o1.getYearNumber() < o2.getYearNumber()) {
					return -1;
				}
				
				//year numbers equal
				if (o1.getSemesterTypeID() > o2.getSemesterTypeID()) {
					return 1;
				}
				if (o1.getSemesterTypeID() < o2.getSemesterTypeID()) {
					return -1;
				}
				
				//semester types equals
				if (o1.getStudiesTypeID() > o2.getStudiesTypeID()) {
					return 1;
				}
				if (o1.getStudiesTypeID() < o2.getStudiesTypeID()) {
					return -1;
				}
				return 0;
			}
		};
		
		Collections.sort(semestersList, comp);
		
		return semestersList.get(semestersList.size()-1).getId();
	}
}
